package com.vint.mi;

import java.util.Arrays;

/**
 * 1.done
 * 2.easy
 * 对一个数组，只要对其一个子数组排序，就可以使整个升序，求子数组的长度
 */
public class Mi58 {
    public static String solution(String line){
        String[] src = line.split(",");
        int[] ints = new int[src.length];
        for(int i=0; i<src.length; i++){
            ints[i] = Integer.parseInt(src[i]);
        }
        return getSortLength(ints) +"";

    }
    public static int getSortLength(int[] src){
        if(src==null||src.length<=1)return 0;
        int[] intBack = new int[src.length];
        for(int i=0; i<src.length; i++){
           intBack[i] = src[i];
        }
        Arrays.sort(src);
        int start = 0;
        int end = 0;
        for(int i = 0; i< src.length; i++){
            if(src[i]!=intBack[i]){
                start = i;
                break;
            }
        }
        for(int i = src.length -1; i>= 0; i--){
            if(src[i]!=intBack[i]){
                end = i;
                break;
            }
        }
        return end - start + 1;

    }

    public static void main(String[] args) {
        //System.out.println(solution("1,2,5,4,7"));
        System.out.println(solution("2,6,4,8,10,9,15"));
    }
}
